﻿using System;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Regions;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Windows;

namespace BizControl.Presentation.Infrastructure.RegionAdapters
{
  [Export]
  public class StackPanelAdapter : RegionAdapterBase<StackPanel>
  {
    [ImportingConstructor]
    public StackPanelAdapter(IRegionBehaviorFactory behaviorFactory) :
      base(behaviorFactory)
    { }

    protected override void Adapt(IRegion region, StackPanel regionTarget)
    {
      region.Views.CollectionChanged += (s, e) =>
        {
          if (e.Action == NotifyCollectionChangedAction.Add)
            foreach (FrameworkElement element in e.NewItems)
              regionTarget.Children.Add(element);
          else if (e.Action == NotifyCollectionChangedAction.Remove)
            foreach (FrameworkElement element in e.NewItems)
              regionTarget.Children.Remove(element);
        };
    }

    protected override IRegion CreateRegion()
    {
      return new AllActiveRegion();
    }
  }
}
